package cmms.utils;

import java.sql.Timestamp;

/**
 * 时间处理工具类
 * @author xk.liu
 *
 */
public class PlotUtils {
	
	/**
	 * 
	 * @param ts 需要矫正的时间
	 * @param minutes 整点对齐的分钟数，如 30
	 * @return
	 */
	public static Timestamp adjustTime(Timestamp ts, int minutes) {
		long scale = minutes * 60000;
		if (ts == null) {
			ts = new Timestamp((System.currentTimeMillis() / scale) * scale);
		} else {
			ts.setTime((ts.getTime() / scale) * scale); 
		}
		
		return ts;
	}
	
	/**
	 * 
	 * @param ts 需要矫正的时间
	 * @param minutes 整点对齐的分钟数，如 30
	 * @param minTime 矫正时间比较的最小时间
	 * @param diffMinutes 矫正时间比较比最小时间大多少分钟
	 * @return
	 */
	public static Timestamp adjustTime(Timestamp ts, int minutes, Timestamp minTime, int diffMinutes) {
		long scale = minutes * 60000;
		if (ts == null) {
			ts = new Timestamp(((minTime.getTime() + diffMinutes * 60000) / scale) * scale);
		} else {
			ts.setTime((ts.getTime() / scale) * scale);
			if (minTime != null) {
				if (ts.getTime() + diffMinutes * 60000 < minTime.getTime()) {
					ts.setTime(((minTime.getTime() + diffMinutes * 60000) / scale) * scale);
				}
			}
		}
		
		return ts;
	}
	
	// plotTime在区间内返回0， 小于区间返回-1，大于区间返回1
	public static int betweenMintue(Timestamp plotTime, long axisTime, int decMinutes, int addMinutes) {
		int result = 0;
		if (plotTime.getTime() < axisTime - decMinutes * 60000){
			result = -1;
		} else if (plotTime.getTime() >= axisTime + addMinutes * 60000) {
			result = 1;
		} else {
			result = 0;
		}
		
		return result;
	}
}
